package com.weatherstation.entity;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import com.fasterxml.jackson.annotation.JsonFormat;

import java.time.LocalDateTime;

/**
 * 设备信息实体类
 * 
 * @author WeatherStation Team
 */
@TableName("device_info")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DeviceInfo {
    
    @TableId(type = IdType.AUTO)
    private Integer id;
    
    /**
     * 设备名称
     */
    @TableField("device_name")
    private String deviceName;
    
    /**
     * 设备类型
     */
    @TableField("device_type")
    private String deviceType;
    
    /**
     * 设备位置
     */
    @TableField("location")
    private String location;
    
    /**
     * 设备状态 1:正常 0:异常
     */
    @TableField("status")
    private Integer status = 1;
    
    /**
     * 最后心跳时间
     */
    @TableField("last_heartbeat")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime lastHeartbeat;
    
    /**
     * 创建时间
     */
    @TableField(value = "created_at", fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdAt;
    
    /**
     * 更新时间
     */
    @TableField(value = "updated_at", fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updatedAt;
    
    /**
     * 构造函数
     */
    public DeviceInfo(String deviceName, String deviceType, String location) {
        this.deviceName = deviceName;
        this.deviceType = deviceType;
        this.location = location;
        this.status = 1;
        this.lastHeartbeat = LocalDateTime.now();
    }
    
    /**
     * 更新心跳时间
     */
    public void updateHeartbeat() {
        this.lastHeartbeat = LocalDateTime.now();
        this.status = 1; // 设置为正常状态
    }
    
    /**
     * 检查设备是否在线
     * @param timeoutMinutes 超时分钟数
     * @return 是否在线
     */
    public boolean isOnline(int timeoutMinutes) {
        if (lastHeartbeat == null) {
            return false;
        }
        return lastHeartbeat.isAfter(LocalDateTime.now().minusMinutes(timeoutMinutes));
    }
}